<?php
namespace app\api\controller;
class File
{
    public function index()
    {
        $dir = '/wwwroot/public';
        //获取某目录下所有文件、目录名（不包括子目录下文件、目录名）
        $handler = opendir($dir);
        while (($filename = readdir($handler)) !== false) {//务必使用!==，防止目录下出现类似文件名“0”等情况
            if ($filename != "." && $filename != "..") {
                if (is_file("$dir/$filename") && $filename != '.DS_Store' && $filename != '.gitignore' && $filename != '.htaccess'){
                    $files[] = [
                        'filename'=>$filename
                    ] ;
                }
            }
        }
        closedir($handler);
        
        return json($files);
    }
    public function upload()
    {
        $file = $_FILES['file'];
        $filename = $file['name'];
        move_uploaded_file($file['tmp_name'], "/wwwroot/public/$filename");
    }
    public function del()
    {
        $filename = '/wwwroot/public/'.request()->param('filename');
        $status = unlink($filename);
        return json($status);
    }
}
